package football.haha.com.footballtv.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import football.haha.com.footballtv.FootballApplication;
import football.haha.com.footballtv.R;
import football.haha.com.footballtv.bean.BeanAd;
import football.haha.com.footballtv.bean.BeanHighLights;
import football.haha.com.footballtv.bean.BeanIndexFootball;
import football.haha.com.footballtv.bean.BeanLookBack;
import football.haha.com.footballtv.bean.BeanSchedule;
import football.haha.com.footballtv.bean.BeanScoreRank;
import football.haha.com.footballtv.bean.BeanSecondFootball;
import football.haha.com.footballtv.bean.BeanShootRank;
import football.haha.com.footballtv.bean.BeanUCSResp;
import football.haha.com.footballtv.utils.ApiUtils;
import football.haha.com.footballtv.utils.ImageToos;
import football.haha.com.footballtv.utils.NetUtils;
import football.haha.com.footballtv.utils.ShareTools;
import football.haha.com.footballtv.utils.Tools;

/**
 * Created by haha on 2017/10/6.
 */

public class LeagueHomeDataActivity extends LeagueHomeViewActivity {


    private BeanSchedule.ContentsBean contentsBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        leagueBean = getIntent().getParcelableExtra("League");
        getHomeData();
    }

    private void getHomeData() {
        Map<String, String> params = new HashMap<>();
        params.put("command", "second_football");
        params.put("busCode", "IPS");
        params.put("leagueMatchCode", leagueBean.getCode());
        NetUtils.getData(this, ApiUtils.HOST_URL, ApiUtils.TAGIndex_football, params, BeanSecondFootball.class, this);
    }


    @Override
    public void getLookbackData() {
        super.getLookbackData();
        if (beanSecondFootball == null) {
            return;
        }
        Map<String, String> params = new HashMap<>();
        params.put("command", "folderContents");
        params.put("folderCode", beanSecondFootball.getLookBack().getBlockCode());
        params.put("busCode", "IPS");
        params.put("pageIndex", pageIndexLookBack + "");
        params.put("pageItems", pageItemsLookBack + "");
        NetUtils.getData(this, ApiUtils.HOST_URL, ApiUtils.TAGLookBack, params, BeanLookBack.class, this);
    }

    @Override
    public void getHighLights() {
        super.getHighLights();
        if (beanSecondFootball == null) {
            return;
        }
        Map<String, String> params = new HashMap<>();
        params.put("command", "folderContents");
        params.put("folderCode", beanSecondFootball.getHighlights().getBlockCode());
        params.put("busCode", "IPS");
        params.put("pageIndex", pageIndexHighLights + "");
        params.put("pageItems", pageItemsHighLights + "");
        NetUtils.getData(this, ApiUtils.HOST_URL, ApiUtils.TAGHighlights, params, BeanHighLights.class, this);
    }

    @Override
    public void getScheduleDayData(String day) {
        super.getScheduleDayData(day);
        if (beanSecondFootball == null) {
            return;
        }
        Map<String, String> params = new HashMap<>();
        params.put("command", "program");
        params.put("date", day);
//        params.put("date", "20171022");
        params.put("folderCode", beanSecondFootball.getSchedule().getBlockCode());
        params.put("busCode", "IPS");
        NetUtils.getData(this, ApiUtils.HOST_URL, ApiUtils.TAGScheduleDayData, params, BeanSchedule.class, null, this);
    }

    @Override
    public void getRankData(String type) {
        Map<String, String> params = new HashMap<>();
        params.put("type", type);
        params.put("competitionId", "1");

        if (TextUtils.equals(type, "shootRank")) {
            NetUtils.postJsonData(this, ApiUtils.OMS_URL, ApiUtils.TAGGetShootRankData, params, BeanShootRank.class, null, this);
        } else {
            NetUtils.postJsonData(this, ApiUtils.OMS_URL, ApiUtils.TAGGetScoreRankData, params, BeanScoreRank.class, null, this);
        }
    }

    @Override
    public void netBack(int requestTag, Object object) {
        super.netBack(requestTag, object);
        try {


            if (requestTag == ApiUtils.TAGIndex_football) {
                beanSecondFootball = (BeanSecondFootball) object;
                if (beanSecondFootball == null) {
                    return;
                }

                tvName.setText(beanSecondFootball.getName());
                ImageToos.setImageWithUrl(beanSecondFootball.getLogoUrl(), ivIcon, FootballApplication.getOptions(R.drawable.default_team));

                List<BeanSecondFootball.IndexBean.RecommendBean.ListBeanX> recList = beanSecondFootball.getIndex().getRecommend().getList();

                if (recList != null && recList.size() > 0) {
                    ImageToos.setImageWithUrl(recList.get(0).getLivePicUrl(), ivFightRecBig, FootballApplication.getOptions(R.drawable.default_video));

                    if (recList.size() > 1) {
                        BeanSecondFootball.IndexBean.RecommendBean.ListBeanX rec1 = recList.get(1);
                        if (TextUtils.equals(rec1.getProgramType(), "1")) {
                            tvFightTip1.setText("直播");
                        } else {
                            tvFightTip1.setText("回看");
                        }
                        tvFightTitle1.setText(rec1.getName());
                    }

                    if (recList.size() > 2) {
                        BeanSecondFootball.IndexBean.RecommendBean.ListBeanX rec2 = recList.get(2);
                        if (TextUtils.equals(rec2.getProgramType(), "1")) {
                            tvFightTip2.setText("直播");
                        } else {
                            tvFightTip2.setText("回看");
                        }
                        tvFightTitle2.setText(rec2.getName());
                    }

                }

                BeanSecondFootball.IndexBean.OperationPositionBean operationPositionBean = beanSecondFootball.getIndex().getOperationPosition();
                if (operationPositionBean != null) {
                    //右侧推荐
                    List<BeanSecondFootball.IndexBean.OperationPositionBean.ListBean> positonList = operationPositionBean.getList();

                    if (positonList != null) {
                        for (int i = 0; i < positonList.size(); i++) {
                            View itemRec = FrameLayout.inflate(this, R.layout.item_rec_big, null);
                            addFocusListener(itemRec);

                            FrameLayout.LayoutParams param = new FrameLayout.LayoutParams((int) getResources().getDimension(R.dimen.px420),
                                    (int) getResources().getDimension(R.dimen.px480));
                            param.rightMargin = (int) getResources().getDimension(R.dimen.px180);
                            int itemMargin = (int) getResources().getDimension(R.dimen.px10);

                            switch (i % 5) {
                                case 0:
                                    param.height = (int) getResources().getDimension(R.dimen.px480);
                                    param.leftMargin = (int) getResources().getDimension(R.dimen.px960) +
                                            (param.width + itemMargin) * (i / 5) * 2;
                                    param.topMargin = (int) getResources().getDimension(R.dimen.px30);
                                    itemRec.setNextFocusUpId(R.id.cb_league_home);
                                    break;
                                case 1:
                                    param.leftMargin = (int) getResources().getDimension(R.dimen.px960) +
                                            (param.width + itemMargin) * (i / 5) * 2;
                                    param.topMargin = (int) getResources().getDimension(R.dimen.px30) + (param.height + itemMargin);
                                    param.height = (int) getResources().getDimension(R.dimen.px235);
                                    break;
                                case 2:
                                    param.height = (int) getResources().getDimension(R.dimen.px235);
                                    param.leftMargin = (int) getResources().getDimension(R.dimen.px960) +
                                            (param.width + itemMargin) * (i / 5 * 2 + 1);
                                    param.topMargin = (int) getResources().getDimension(R.dimen.px30);
                                    itemRec.setNextFocusUpId(R.id.cb_league_home);
                                    break;
                                case 3:
                                    param.height = (int) getResources().getDimension(R.dimen.px235);
                                    param.leftMargin = (int) getResources().getDimension(R.dimen.px960) +
                                            (param.width + itemMargin) * (i / 5 * 2 + 1);
                                    param.topMargin = (int) getResources().getDimension(R.dimen.px275);
                                    break;
                                case 4:
                                    param.height = (int) getResources().getDimension(R.dimen.px235);
                                    param.leftMargin = (int) getResources().getDimension(R.dimen.px960) +
                                            (param.width + itemMargin) * (i / 5 * 2 + 1);
                                    param.topMargin = (int) getResources().getDimension(R.dimen.px520);
                                    break;

                            }


                            ImageView ivRec = (ImageView) itemRec.findViewById(R.id.iv_rec);
                            TextView tvRecTitle = (TextView) itemRec.findViewById(R.id.tv_rec_title);
                            BeanSecondFootball.IndexBean.OperationPositionBean.ListBean posData = positonList.get(i);
                            if (posData.getMedia() != null) {
                                ImageToos.setImageWithUrl(posData.getMedia().getImages().get(0).getUrl(), ivRec,
                                        FootballApplication.getOptions(R.drawable.default_video));
                                tvRecTitle.setText(posData.getMedia().getName());
                            } else {
                                ImageToos.setImageWithUrl(posData.getPic().getPicUrl(), ivRec,
                                        FootballApplication.getOptions(R.drawable.default_video));
                                tvRecTitle.setText(posData.getPic().getName());
                            }
                            homeRecContentList.add(itemRec);
                            layoutLeagueHomeContent.addView(itemRec, param);
                        }
                    }
                }
            } else if (requestTag == ApiUtils.TAGLookBack) {
                beanLookBack = (BeanLookBack) object;
                if (beanLookBack == null) {
                    return;
                }
                addLookbackVideo(beanLookBack.getContents());
            } else if (requestTag == ApiUtils.TAGHighlights) {
                beanHighLights = (BeanHighLights) object;
                if (beanHighLights == null) {
                    return;
                }
                addHighLights(beanHighLights.getContents());
            } else if (requestTag == ApiUtils.TAGScheduleDayData) {
                BeanSchedule beanSchedule = (BeanSchedule) object;
                if (beanSchedule == null) {
                    return;
                }
                scheduleContentList = beanSchedule.getContents();
                addScheduleContentList(scheduleContentList);
            } else if (requestTag == ApiUtils.TAGAddORDERData) {
                BeanUCSResp beanUCSResp = (BeanUCSResp) object;
                if (TextUtils.equals(beanUCSResp.getResultCode(), "0")) {
                    showToast("预约成功");
                    operateOrderShareStr();
                } else {
                    showToast(beanUCSResp.getResultMsg());
                }
            } else if (requestTag == ApiUtils.TAGDelORDERData) {
                BeanUCSResp beanUCSResp = (BeanUCSResp) object;
                if (TextUtils.equals(beanUCSResp.getResultCode(), "0")) {
                    showToast("取消预约成功");
                    operateOrderShareStr();
                } else {
                    showToast(beanUCSResp.getResultMsg());
                }
            } else if (requestTag == ApiUtils.TAGGetScoreRankData) {
                BeanScoreRank beanScoreRank1 = (BeanScoreRank) object;
                if (beanScoreRank1.isIsOK()) {
                    beanScoreRank = beanScoreRank1;
                    addRankIntegration();
                }
            } else if (requestTag == ApiUtils.TAGGetShootRankData) {
                BeanShootRank beanShootRank1 = (BeanShootRank) object;
                if (beanShootRank1.isIsOK()) {
                    beanShootRank = beanShootRank1;
                    addRankMan();
                }
            }
        } catch (Exception ex) {
            showToast("敬请期待");
            ex.printStackTrace();
        }

    }

    public void orderData(int index) {
        contentsBean = scheduleContentList.get(index);
        if (Tools.isOrder(this, contentsBean.getContentId())) {
            Map<String, String> params = new HashMap<>();
            params.put("requestType", "3");
            params.put("command", "4");
            params.put("userId", Tools.getDeviceId(this));
            params.put("scheduleId", contentsBean.getContentId());

            NetUtils.postJsonData(this, ApiUtils.UCS_URL, ApiUtils.TAGDelORDERData, params, BeanUCSResp.class, "取消预约中...", this);
        } else {
            Map<String, String> params = new HashMap<>();
            params.put("requestType", "3");
            params.put("command", "1");
            params.put("userId", Tools.getDeviceId(this));
            params.put("scheduleId", contentsBean.getContentId());
            params.put("scheduleDes", contentsBean.getName());
            params.put("beginTime", contentsBean.getStartTime());

            NetUtils.postJsonData(this, ApiUtils.UCS_URL, ApiUtils.TAGAddORDERData, params, BeanUCSResp.class, "正在预约中...", this);
        }

    }

    private void operateOrderShareStr() {
        int operateIndex = scheduleContentList.indexOf(contentsBean);
        View view = scheduleContentItemList.get(operateIndex);
        TextView tvOrderState = (TextView) view.findViewById(R.id.tv_order_state);
        String contentId = contentsBean.getContentId();
        String contentIdStr = ShareTools.getStringPreference(this, "contentIdStr");
        if (Tools.isOrder(this, contentId)) {
            contentIdStr = contentIdStr.replace(contentId + "|", "");
            tvOrderState.setText("");
        } else {
            contentIdStr = contentIdStr + contentId + "|";
            tvOrderState.setText("已预约");
        }
        ShareTools.setStringPreference(this, "contentIdStr", contentIdStr);

    }

}
